package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderCriteria;
import com.shrct.businessprocess.purchase.RCPurchaseOrderStatus;
import edu.fudan.mylang.pf.query.HQLBasedPagedQueryCriteria;
import org.hibernate.Query;

public class RCPurchaseOrderCriteriaImpl extends HQLBasedPagedQueryCriteria<RCPurchaseOrder> implements RCPurchaseOrderCriteria {
  private String orderNumber;
  
  public String getOrderNumber() {
    return this.orderNumber;
  }
  
  public RCPurchaseOrderCriteria setOrderNumber(final String orderNumber) {
    this.orderNumber = orderNumber;
    return this;			
    
  }
  
  private RCProject project;
  
  public RCProject getProject() {
    return this.project;
  }
  
  public RCPurchaseOrderCriteria setProject(final RCProject project) {
    this.project = project;
    return this;			
    
  }
  
  private RCSupplier supplier;
  
  public RCSupplier getSupplier() {
    return this.supplier;
  }
  
  public RCPurchaseOrderCriteria setSupplier(final RCSupplier supplier) {
    this.supplier = supplier;
    return this;			
    
  }
  
  private RCPurchaseOrderStatus closedStatus;
  
  public RCPurchaseOrderStatus getClosedStatus() {
    return this.closedStatus;
  }
  
  public RCPurchaseOrderCriteria setClosedStatus(final RCPurchaseOrderStatus closedStatus) {
    this.closedStatus = closedStatus;
    return this;			
    
  }
  
  private RCPurchaseOrderStatus cancelStatus;
  
  public RCPurchaseOrderStatus getCancelStatus() {
    return this.cancelStatus;
  }
  
  public RCPurchaseOrderCriteria setCancelStatus(final RCPurchaseOrderStatus cancelStatus) {
    this.cancelStatus = cancelStatus;
    return this;			
    
  }
  
  private RCEmployee owner;
  
  public RCEmployee getOwner() {
    return this.owner;
  }
  
  public RCPurchaseOrderCriteria setOwner(final RCEmployee owner) {
    this.owner = owner;
    return this;			
    
  }
  
  private RCEmployee businessMan;
  
  public RCEmployee getBusinessMan() {
    return this.businessMan;
  }
  
  public RCPurchaseOrderCriteria setBusinessMan(final RCEmployee businessMan) {
    this.businessMan = businessMan;
    return this;			
    
  }
  
  protected String buildHQLFrom() {
    return " from com.shrct.businessprocess.purchase.impl.RCPurchaseOrderImpl this  left join  this.purchaseRequest.budget.cbd.project.salers as salerAssignment ";
  }
  
  protected String buildHQLBody() {
    String hql = "where 1 = 1 ";
    	hql += " and ((this.isInitialized) = (true))" ;
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOrderNumber()) ){
    	hql += " and (this.orderNumber  LIKE :orderNumber)" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getProject()) ){
    	hql += " and ((this.purchaseRequest.budget.cbd.project) = (:project))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getSupplier()) ){
    	hql += " and ((this.supplier) = (:supplier))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getClosedStatus()) ){
    	hql += " and ((this.Status) <> (:closedStatus))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getCancelStatus()) ){
    	hql += " and ((this.Status) <> (:cancelStatus))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOwner()) && !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOwner()) && !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOwner()) ){
    	hql += " and ((((this.purchaseRequest.budget.cbd.project.customer.businessMan) = (:owner)) or ((salerAssignment.employee) = (:owner))) or ((salerAssignment.employee.directManager) = (:owner)))" ;
    }
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getBusinessMan()) ){
    	hql += " and ((this.purchaseRequest.budget.cbd.project.customer.businessMan) = (:businessMan))" ;
    }
    return hql;
    
  }
  
  public void initOrderBys() {
    addOrder("this.orderNumber",edu.fudan.mylang.pf.query.OrderItem.DESC);
  }
  
  protected void fillParameter(final Query query) {
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOrderNumber())){
    	query.setParameter("orderNumber",getOrderNumber()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getProject())){
    	query.setParameter("project",getProject()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getSupplier())){
    	query.setParameter("supplier",getSupplier()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getClosedStatus())){
    	query.setParameter("closedStatus",getClosedStatus()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getCancelStatus())){
    	query.setParameter("cancelStatus",getCancelStatus()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getOwner())){
    	query.setParameter("owner",getOwner()) ;
    }			
    if( !edu.fudan.mylang.pf.query.QueryUtil.isNull(getBusinessMan())){
    	query.setParameter("businessMan",getBusinessMan()) ;
    }			
    
  }
  
  protected Boolean isDistinct() {
    return true;
  }
  
  public RCPurchaseOrderCriteriaImpl() {
    initOrderBys();
  }
}
